package com.yc.pre.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @Description 阿里短信
 * @Date 2020/11/25 17:30:11
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class AliSMSUtil {
    private static String AccessKeyId = "LTAI4GG8obU7B6D9G1TfomL8";//对应的accesskeyid
    private static String AccessKeySecret = "ZvNPEEZ9OSRnAiooXlI2dbM7ScNCnY";//对应的accesskeysecret

    public static String sendMessage(String phone, String code){  //传入手机号，一次调用支持发送一个手机号验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");//日期别乱写
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);//手机号
        request.putQueryParameter("SignName", "浙商资产");//签名名称

        request.putQueryParameter("TemplateCode", "SMS_194755067");//模板code
        request.putQueryParameter("TemplateParam","{\"code\":\"" + code + "\"}");//这个里面填写验证码内容
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            System.out.println(response.getData());
            return jsonObject.get("Code").toString();
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改密码模板
     * @param phone
     * @param code
     * @return
     */
    public static String sendModifyPasswordMessage(String phone, String code){  //传入手机号，一次调用支持发送一个手机号验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");//日期别乱写
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);//手机号
        request.putQueryParameter("SignName", "浙商资产");//签名名称

        request.putQueryParameter("TemplateCode", "SMS_194755064");//模板code
        request.putQueryParameter("TemplateParam","{\"code\":\"" + code + "\"}");//这个里面填写验证码内容
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            System.out.println(response.getData());
            return jsonObject.get("Code").toString();
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改信息
     * @param phone
     * @param code
     * @return
     */
    public static String sendModifyMessage(String phone, String code){  //传入手机号，一次调用支持发送一个手机号验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");//日期别乱写
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);//手机号
        request.putQueryParameter("SignName", "浙商资产");//签名名称

        request.putQueryParameter("TemplateCode", "SMS_194755063");//模板code
        request.putQueryParameter("TemplateParam","{\"code\":\"" + code + "\"}");//这个里面填写验证码内容
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            System.out.println(response.getData());
            return jsonObject.get("Code").toString();
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        //sendMessage("17630829902", "123456");
        sendModifyMessage("17630829902", "123456");
    }
}
